#include "myqlistwidget.h"
#include <QScrollBar>
#include <QPropertyAnimation>


MyQListWidget::MyQListWidget(QWidget *parent) : QListWidget(parent)
{

}

void MyQListWidget::wheelEvent(QWheelEvent *event)
{
    QScrollBar *scrollBar = verticalScrollBar();
    if (scrollBar) {
        //获取鼠标滚轮的垂直滚动量，主要获取值的正负数
        int scrollAmount = event->angleDelta().y();

        int scrollnum;
        //鼠标滚轮向上滚动，scrollAmount为正,q
        if (scrollAmount >0){
            scrollnum = -1;
        }
        else{
            scrollnum = 1;
        }
        
        //获取当前滚动条的值
        int currentValue = scrollBar->value();
        //滚条值变大向下，变小向上
        int newValue = currentValue + scrollnum;


        QPropertyAnimation *animation = new QPropertyAnimation(scrollBar, "value");
        animation->setDuration(100);  // 动画持续时间，可调整
        animation->setStartValue(currentValue);
        animation->setEndValue(newValue);
        animation->setEasingCurve(QEasingCurve::OutQuad);  // 可使用不同的缓动曲线，使滚动更平滑
        animation->start(QAbstractAnimation::DeleteWhenStopped);


        event->accept();
    } else {
        QListWidget::wheelEvent(event);
    }
}
